// 城市列表页
(function () {
  if (location.pathname !== '/location') {
    return false;
  }
  // 自动定位
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (geo) {
      $.get('/location/ajax/geosearch', {
        lat: geo.coords.latitude,
        lng: geo.coords.longitude
      }, function (res) {
        if (res.code === 200) {
          $('#autoloc .text').html(res.data.name).addClass('text-orange');
          $('#autoloc').prop('href', '/location/' + res.data.id + '?next=' + encodeURIComponent($('#autoloc').data('next')));
        }
        else {
          $('#autoloc .text').html(res.data.message);
        }
      });
    }, function () {
      $('.geo .list').html('<div class="item oneline text-gray">获取位置信息失败</div>');
    });
  }

  // 打开测试社区
  var times = 0;
  $(document).on('click', '#anchor', function () {
    times += 1;
    if (times >= 3) {
      $('.hide').removeClass('hide');
    }
  });
})();

// 社区列表页
(function () {
  if (!/\/location\/\d+/.test(location.pathname)) {
    return false;
  }
  var timer = null;
  var delay = 200;
  var list = $('.projects');

  function filter(query) {
    list.forEach(function (project) {
      var count = 0;
      $(project).find('.item').forEach(function (item) {
        var match = $(item).html().indexOf(query) !== -1;
        count += match ? 1 : 0;
        $(item).toggle(match > 0);
      });
      $(project).toggle(count > 0);
    });
  }

  $('input[name="name"]').on('input', function () {
    clearTimeout(timer);
    timer = setTimeout(filter, delay, $(this).val());
  });
})();
